package com.bigger.arch.app.repository.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

@Configuration
//@PropertySource("classpath:application.properties")
//@EnableJpaRepositories(basePackages = "com.bigger.arch.dao.dao")
public class BiggerDaoConfig {

    private DataSource createDataSource(String driver, String url, String username, String password) {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass(driver);
            dataSource.setJdbcUrl(url);
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setAutoCommitOnClose(false);
            dataSource.setInitialPoolSize(10);
            dataSource.setMinPoolSize(10);
            dataSource.setMaxPoolSize(100);
            return dataSource;
        } catch (Exception exception) {
            //todo:这边可以自定义DaoException
            throw new RuntimeException(exception);
        }
    }

/*    @Value("${spring.datasource.driver-class-name}")
    private String driver;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;*/

    @Autowired
    private Environment environment;

    @Bean("db_test")
    public DataSource dataSource_db_test() {
        String driver = environment.getProperty("spring.datasource.driver-class-name");
        String url = environment.getProperty("spring.datasource.url");
        String username = environment.getProperty("spring.datasource.username");
        String password = environment.getProperty("spring.datasource.password");
        return createDataSource(driver, url, username, password);
    }

    @Bean("jdbc_db_test")
    public JdbcTemplate jdbcTemplate_db_test() {
        return new JdbcTemplate(dataSource_db_test());
    }

    @Bean("named_jdbc_db_test")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate_db_test() {
        return new NamedParameterJdbcTemplate(dataSource_db_test());
    }

/*    @Bean("tran_db_test")
    public DataSourceTransactionManager dataSourceTransactionManager() {
        return new DataSourceTransactionManager(dataSource_db_test());
    }*/

    /*@Bean("jpa_factory_db_test")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean_db_test() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource_db_test());
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("com.bigger.arch.dao.model");
        return entityManagerFactoryBean;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.MYSQL);
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(false);
        jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL8Dialect");
        return jpaVendorAdapter;
    }*/

/*    @Bean
    public JpaTransactionManager jpaTransactionManager() {
        return new JpaTransactionManager((EntityManagerFactory) entityManagerFactoryBean_db_test());
    }*/

}
